<template>
  <div class="header">
    <div class="language">繁体中文</div>
    <div class="user-info_box">
      <img :src="avatar" class="avatar" />
      <div class="nickname">{{ mail }}</div>
    </div>
    <button class="logout" @click="logout" v-loading="loading">退出登录</button>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { reqLoginOut } from "@/api/index";
import { ref } from "vue";
import { useUserStore } from "@/store/index";
import { storeToRefs } from "pinia";
import defaultAvatar from "@/assets/image/default-avatar.png";

const router = useRouter();
const loading = ref(false);
const userStore = useUserStore();
const mail = storeToRefs(userStore).mail;
const avatar = ref(defaultAvatar);

const logout = async () => {
  try {
    loading.value = true;
    const loginOutRes = await reqLoginOut(localStorage.getItem("token"));
    clog("登出成功：", loginOutRes);
    loading.value = false;
    localStorage.clear();
    router.push({ path: "/login" });
  } catch (e) {
    clog("登出失败：", e);
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
.header {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 128px;
  background: #ffffff;
  box-shadow: 0px 4px 8px 0px #00000016;
  padding-right: 45px;
  flex-shrink: 0;

  .language {
    color: #444444;
    font-weight: bold;
  }

  .user-info_box {
    display: flex;
    align-items: center;
    margin-left: 60px;

    .avatar {
      border-radius: 10px;
      width: 32px;
      height: 32px;
    }

    .nickname {
      color: #444444;
      font-weight: bold;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      margin-left: 16px;
    }
  }

  .logout {
    width: 138px;
    height: 49px;
    text-align: center;
    line-height: 49px;
    border-radius: 3px;
    background: #2fc2af;
    color: #ffffff;
    font-weight: bold;
    margin-left: 38px;
    outline: none;
    border: none;
    cursor: pointer;
    font-size: 18px;

    &:hover {
      opacity: 0.8;
    }
  }
}
</style>
